package cn.gov.msa.util;

import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import net.sf.json.JSONObject;

//import com.msa.msaportal.entity.UserComponent;

public class WebServiceTools {//这个类应该可以直接用
	
	/**
	 * 根据url得到返回数据
	 * 固定访问名称为
	 * @param url
	 * @return
	 * @throws DocumentException 
	 * @throws XmlPullParserException 
	 * @throws IOException 
	 */
	public static String getDataByUrl(String url,String methods,String parameter) throws DocumentException, IOException, XmlPullParserException {
		
		String nameSpace="";
		String result="";
	
			nameSpace = gettargetNamespace(url);
			result =getRemoteInfo(nameSpace,url,methods, parameter);
		
		return result;
	}
	
	/**
	 * 根据多个参数获取返回数据
	 * @param url
	 * @param methods
	 * @param parameter
	 * @return
	 * @throws DocumentException
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public static String getDatasByUrl(String url,String methods,String[] parameter) throws DocumentException, IOException, XmlPullParserException {
		
		String nameSpace="";
		String result="";
	
			nameSpace = gettargetNamespace(url);
			result =getRemoteInfo(nameSpace,url,methods, parameter);
		
		return result;
	}
	
	/**
	 * 根据wsdl地址得到命名空间
	 * @param url
	 * @return
	 * @throws DocumentException
	 */
	public static String gettargetNamespace(String url) throws DocumentException{
		SAXReader reader = new SAXReader();
		 Document   document = reader.read(url); 
		 Element root = document.getRootElement(); 
		return  root.attributeValue("targetNamespace");
	}
	/**
	 * 根据多个参数获取命名空间
	 * @param targetNamespace
	 * @param url
	 * @param methods
	 * @param parameter
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public static String getRemoteInfo(String targetNamespace,String url,String methods,String[] parameter) throws IOException, XmlPullParserException {
		String urls=url.replace("?wsdl", "");
		// 命名空间
		String nameSpace = targetNamespace;
		// 调用的方法名称
		String methodName = methods;
		// EndPoint
		String endPoint = url;
		// SOAP Action
		String soapAction = targetNamespace+urls;
		// 指定WebService的命名空间和调用的方法名
		SoapObject rpc = new SoapObject(nameSpace, methodName);
		for (int i = 0; i < parameter.length; i++) {
			rpc.addProperty(
					"arg"+i,
					parameter[i]);
		}
		
		// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.bodyOut = rpc;
		// 设置是否调用的是dotNet开发的WebService
		envelope.dotNet = false;
		// 等价于envelope.bodyOut = rpc;
		envelope.setOutputSoapObject(rpc);

		HttpTransportSE transport = new HttpTransportSE(endPoint);
		
			// 调用WebService
			transport.call(soapAction, envelope);
	

		// 获取返回的数据
		SoapObject object = (SoapObject) envelope.bodyIn;
		// 获取返回的结果
		String result = object.getProperty(0).toString();
		//System.out.println(result);
		return result;
		// 将WebService返回的结果显示在TextView中
	}
	
	public static String getRemoteInfo(String targetNamespace,String url,String methods,String parameter) throws IOException, XmlPullParserException {
		String urls=url.replace("?wsdl", "");
		// 命名空间
		String nameSpace = targetNamespace;
		// 调用的方法名称
		String methodName = methods;
		// EndPoint
		String endPoint = url;
		// SOAP Action
		String soapAction = targetNamespace+urls;
		// 指定WebService的命名空间和调用的方法名
//		System.out.println(nameSpace + "  " +methodName  + "  " + endPoint +"  "+parameter);
		SoapObject rpc = new SoapObject(nameSpace, methodName);
		rpc.addProperty(
				"arg0",
				parameter);
		// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.bodyOut = rpc;
		// 设置是否调用的是dotNet开发的WebService
		envelope.dotNet = false;
		// 等价于envelope.bodyOut = rpc;
		envelope.setOutputSoapObject(rpc);

		HttpTransportSE transport = new HttpTransportSE(endPoint,60000);

		
			// 调用WebService
			
			transport.call(soapAction, envelope);
			
	

			SoapObject object = (SoapObject) envelope.bodyIn;
			// 获取返回的结果
			String result = object.getProperty(0).toString();
			//System.out.println(result);
			return result;
			// 将WebService返回的结果显示在TextView中
	}
	public static void main(String[] args) throws DocumentException, IOException, XmlPullParserException {
		String wsdl1="http://198.32.128.8:7001/HydWorkService/HydWorkServiceService?WSDL";
		String wsdl2="http://198.32.128.8:7001/AdmWorkService/AdmWorkServiceService?WSDL";
		String wsdl3="http://198.32.128.117:7003/gahCMP/GahServiceImplService?WSDL";

//		String method ="hydWork";  // 水上水下活动（新增/修改）数据接收接口
//		String param = "{\"hydwork\": {\"applyid\": \"ACAAFE0A-335A-436F-8BC3-CA7379A9EFAC\",\"applyno\": \"DZZW201800109347\",\"workname\": \"项目名称\",\"worktypecode\": \"03\",\"workbegintime\": \"2018-05-23 12:35:31\",\"workendtime\": \"2018-05-23 12:35:31\",\"buildunitname\": \"建设单位\",\"workunitname\": \"施工单位\",\"workstep\": \"施工作业程序\",\"workarea\": \"地点范围\",\"workpositioncode\": \"1\",\"workmethodcode\": \"4\",\"applytime\": \"2018-08-23 12:35:31 \",\"orgcode\": \"060100\",\"workship\": \"测试01,测试02\"}}";

		String method ="portShipWork";  // 船舶在港区水域内安全作业备案（新增/修改）数据接收接口
		String param = "{\"portshipwork\":{\"applyid\":\"134d0d83-be7a-43bb-bf3d-9c39d6da1222\",\"cargoloading\":\"\",\"berth\":\"泊位\",\"berthcode\":\"0600001\",\"workstarttime\":\"2018-05-23 12:35:31\",\"workendtime\":\"2018-05-23 12:35:31\",\"workship\":\"测试1\",\"worktype\":\"01，02\",\"workunitname\":\"作业单位\",\"reportperson\":\"报备人\",\"reportphone\":\"15650052345\",\"contactperson\":\"\",\"contactphone\":\"15650052346\",\"orgcode\":\"060101\",\"safetymeasure\":\"安全措施\",\"securitystatement\":\"船长安全声明\",\"workcontent\":\"作业部位及项目\",\"correctionopinions\":\"整改意见\",\"workperson\":\"作业人员姓名及单位\"}}";

//		String method ="rivershipwork";  // 内河通航水域安全作业备案（新增/修改）数据接收接口
//		String param = "{\"rivershipwork\":{\"applyid\":\"134d0d83-be7a-43bb-bf3d-9c39d6da1222\",\"business\":\"作业项目\",\"wordaddress\":\"作业地点\",\"workbegintime\":\"2018-05-23 12:35:31 \",\"workendtime\":\"2018-05-23 12:35:31 \",\"workunitname\":\"作业单位\",\"workship\":\"测试1\",\"workcontactperson \":\"作业单位联系人\",\"workcontactphone\":\"作业单位联系电话 \"}}";

//		String method ="hydInspect";  // 内河通航水域安全作业备案（新增/修改）数据接收接口
//		String param = "{\"hydInspect\":{\"result_no\":\"20180523123531862060000\",\"work_no\":\"A31D6FE9-6E83-4B66-BFC3-D67F5F65321D\",\"task_id\":\"S05920180523123532024060000\",\"project_name\":\"项目名称\",\"inspect_unit\":\"江苏海事局\",\"inspected_unit\":\"被检查单位\",\"inspector_name\":\"郎玉生,胜男\",\"inspector_code\":\"777777777,999999999\",\"inspector_phone\":\"\",\"inspect_date\":\"2018-05-23 00:00:00\",\"data_source\":\"0\",\"remark\":\"\",\"org_code\":\"060000\",\"creator_code\":\"06000000005\",\"create_time\":\"2018-05-23 12:35:32\",\"operator_code\":\"06000000005\",\"operate_time\":\"2018-05-23 12:35:31\",\"operate_flag\":\"I\"},\"hydinspectcontents\":[{\"result_no\":\"20180523123531862060000\",\"serial_no\":1,\"content_code\":\"1001\",\"content_name\":\"是否建立并落实了安全生产责任制:\",\"required_flag\":\"1\",\"remark\":\"\",\"creator_code\":\"06000000005\",\"create_time\":\"2018-05-23 12:35:31\",\"operator_code\":\"06000000005\",\"operate_time\":\"2018-05-23 12:35:31\",\"operate_flag\":\"I\"},{\"result_no\":\"20180523123531862060000\",\"serial_no\":2,\"content_code\":\"1002\",\"content_name\":\"作业是否经许可或与许可的作业时间、水域范围、内容是否一致:\",\"required_flag\":\"1\",\"remark\":\"\",\"operator_code\":\"06000000005\",\"create_time\":\"2018-05-23 12:35:31\",\"creator_code\":\"06000000005\",\"operate_time\":\"2018-05-23 12:35:31\",\"operate_flag\":\"I\"}]}";
		
//		String method ="adminInspect";  // 内河通航水域安全作业备案（新增/修改）数据接收接口
//		String param = "{\"adminInspect\":{\"inspect_no\":\"20180709163206324050000\",\"ship_no\":\"A060003000008\",\"task_id\":\"S05920180614104604681050000\",\"work_unit\":\"检查单位1\",\"inspect_date\":\"2018-05-23 00:00:00\",\"inspect_point\":\"0500003\",\"inspector\":\"张三\",\"inspector_phone\":\"18635611090\",\"create_time\":\"2018-05-23 12:35:32\",\"creator_code\":\"06000000005\",\"data_source\":\"0\",\"org_code\":\"060000\",\"operator_code\":\"06000000005\",\"operate_time\":\"2018-05-23 12:35:31\",\"operate_mark\":\"I\",\"inspected_unit\":\"检查单位1\",\"work_no\":\"20180613150340949050000\"},\"adminInspectdetail\":[{\"inspect_no\":\"20180709163206324050000\",\"serial_no\":1,\"content_code\":\"020303\",\"content_name\":\"需要配备应急拖船的，应急拖船是否到位:\",\"remark\":\"\",\"creator_code\":\"06000000005\",\"create_time\":\"2018-05-23 12:35:31\",\"operator_code\":\"06000000005\",\"operate_time\":\"2018-05-23 12:35:31\",\"operate_mark\":\"I\"}]}";

		JSONObject json = JSONObject.fromObject(param);
		String returnMsg = getDataByUrl(wsdl2, method, json.toString());
		System.out.println(returnMsg);
	}
}
